package com.kang.commons.util;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: fawukang
 * @description: 发送短信工具类
 * @author: K.faWu
 * @create: 2021-09-24 15:29
 **/
@Slf4j
@PropertySource(value = "classpath:application.yml")
@ConfigurationProperties(prefix = "tencent")
@Component
public class SMSUtil {

    public static final String REGISTER = "register"; // 注册
    public static final String LOGIN = "login"; // 登录
    public static final String BIRTH = "birth"; // 生日祝福

    // 短信应用SDK AppID
    @Value("${appid}")
    private int appid; // 1400开头

    // 短信应用SDK AppKey
    @Value("${appkey}")
    private String appkey;

    // 短信模板ID，需要在短信应用中申请
    @Value("${registerId}")
    private int registerId; // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请
    @Value("${loginId}")
    private int loginId;
    @Value("${birthId}")
    private int birthId;

    // 短信签名内容
    //@Value("${sign}")
    private String smsSign="法兀IT学习资料"; // NOTE: 这里的签名"腾讯云"只是一个示例，真实的签名需要在短信控制台中申请，另外签名参数使用的是`签名内容`，而不是`签名ID`

    // 验证码
    private String verificationCode = "000000";

    public String sendBirthSms(String phone,String username){
        try{
            String[] params = {username};
            SmsSingleSender smsSingleSender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = smsSingleSender.sendWithParam("86", phone, birthId, params, smsSign, "", "");

            log.info("发送短信 = " + result);
            System.out.println("result = " + result);

            // 发送成功则给验证码赋值
            if (result.result == 0) {
                return "success";
            }else{
                return result.errMsg;
            }
        }catch (IOException | HTTPException e) {
            e.printStackTrace();
            return "发送失败！";
        }
    }

    public String sendSms(String phone,String time, String type){
        try{
            // 随即6位数赋值验证码
            verificationCode = (int) ((Math.random() * 9 + 1) * 100000) + "";

            // 数组具体的元素个数和模板中变量个数必须一致
            // 比如你模板中需要填写验证码和有效时间，{1}，{2}
            // 那你这里的参数就应该填两个
            String[] params = {verificationCode , time};
            SmsSingleSender ssender = new SmsSingleSender(appid,appkey);

            //根据不同的需求发送不同的短信
            int templateId = 0;
            if (REGISTER.equals(type)){
                templateId = this.registerId;
            }else if (LOGIN.equals(type)){
                templateId = this.loginId;
            }else if (BIRTH.equals(type)){
                templateId = this.birthId;
            }
            // 签名参数未提供或者为空时，会使用默认签名发送短信
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone, templateId, params, smsSign, "", "");

            log.info("短信发送结果 = " + result);

            // 发送成功则给验证码赋值
            if (result.result == 0) {
                return verificationCode;
            }else{
                return result.errMsg;
            }
        } catch (IOException | HTTPException e) {
            e.printStackTrace();
        }
            return null;
    }
}
